package com.caration.shell.workspace3D;

import java.util.ArrayList;

import android.appwidget.AppWidgetHostView;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.view.View;

import com.caration.shell.cr3d.SharedData;
import com.caration.shell.item.AppWidgetInfo;
import com.caration.shell.utils.b;
import com.caration.shell.utils.MaskFilterUtils;
import com.caration.shell.workspace2D.ScreenLayout;

public class WidgetsManager {
	public Runnable a = new Runnable() {

		@Override
		public void run() {
			if (!mWidgets.isEmpty()) {
				final int size = mWidgets.size();
				for (int i = 0; i < size; i++) {
					AppWidgetInfo widgetInfo = mWidgets.get(i);
					if (widgetInfo == null)
						continue;
					if (!widgetInfo.isDisable()) {
						AppWidgetHostView hostView = widgetInfo.mHostView;
						if (hostView != null
								&& hostView.getParent() != null
								&& ((ScreenLayout) hostView.getParent())
										.getVisibility() == View.VISIBLE)
							b(widgetInfo);
					} else {
						mWidgets.remove(i);
					}
				}

			}
		}
	};
	private boolean b = false;
	private ArrayList<AppWidgetInfo> c = new ArrayList<AppWidgetInfo>();
	private ArrayList<AppWidgetInfo> mWidgets = new ArrayList<AppWidgetInfo>();
	private Handler mHandler = new Handler();
	private MaskFilterUtils f = new MaskFilterUtils();

	private Bitmap createIcon(AppWidgetInfo widgetInfo, int paramInt) {
		Bitmap bitmap = null;
		if (widgetInfo.mHostView != null) {
			AppWidgetHostView hostView = widgetInfo.mHostView;
			Point pt = new Point(hostView.getWidth(), hostView.getHeight());
			if ((hostView.getLayoutParams() instanceof ScreenLayout.LayoutParams)
					&& pt.x > 0 && pt.y > 0) {
				bitmap = widgetInfo.getBitmap(pt.x, pt.y);
				Canvas cvs = new Canvas(bitmap);
				cvs.drawColor(0, PorterDuff.Mode.CLEAR);
				hostView.draw(cvs);
				switch (paramInt) {
				default:
					break;
				case 1:
					this.f.a(bitmap, cvs);
				}
			}
		}
		return (Bitmap) bitmap;
	}

	public final AppWidgetInfo a(View paramView) {
		int i = this.c.size();
		int j = 0;
		AppWidgetInfo widgetInfo;
		while (true)
			if (j < i) {
				widgetInfo = (AppWidgetInfo) this.c.get(j);
				if (widgetInfo.mHostView == paramView)
					break;
				j++;
				continue;
			} else {
				widgetInfo = null;
			}
		return widgetInfo;
	}

	public final void a() {
		this.b = true;
	}

	public final void a(int screen) {
		int j = this.c.size();
		for (int i = 0; i < j; i++) {
			AppWidgetInfo widgetInfo = this.c.get(i);
			if ((widgetInfo.mScreen != screen)
					|| (widgetInfo.mHostView == null))
				continue;
			widgetInfo.mIcon = createIcon(widgetInfo, 0);
		}
	}

	public final void a(AppWidgetInfo widgetInfo) {
		if (!mWidgets.contains(widgetInfo))
			mWidgets.add(widgetInfo);
		if ((this.b) && (widgetInfo.f())) {
			mHandler.removeCallbacks(this.a);
			mHandler.post(this.a);
		}
	}

	public final boolean a(final AppWidgetInfo widgetInfo, final int paramInt) {
		boolean bool = false;
		if ((widgetInfo.mHostView != null) && (!widgetInfo.isDisable())) {
			if (!widgetInfo.r) {
				widgetInfo.mHostView.b();
				final Bitmap icon = createIcon(widgetInfo, paramInt);
				widgetInfo.mHostView.c();
				if (icon != null) {
					final boolean flag = bool = (widgetInfo.p != null)
							&& (widgetInfo.p.a != null) && (!widgetInfo.p.b());
					// localObject =
					// new c(this, widgetInfo, (Bitmap) localObject,
					// paramInt, bool);
					Runnable r = new Runnable() {

						@Override
						public void run() {
							try {
								if (!widgetInfo.isDisable()) {
									ScreenLayout.LayoutParams layoutparams = (ScreenLayout.LayoutParams) widgetInfo.mHostView
											.getLayoutParams();
									if (widgetInfo.p == null) {
										de de1 = new de();
										widgetInfo.p = de1;
										de1.setWidget(widgetInfo);
									}
									widgetInfo.p.a(layoutparams, icon, flag);
								}
								aaa(widgetInfo, paramInt);
								// _L2:
								return;
								// JVM INSTR pop ;
								// a a3 = a;
								// b b1 = b;
								// c;
								// com.tsf.shell.workspace3D.a.a(a3, b1, d);
								// a a2 = a;
								// b b3 = b;
								// c;
								// com.tsf.shell.workspace3D.a.a(a2, b3, d);
								// if(true) goto _L2; else goto _L1
								// _L1:
								// Exception exception;
								// exception;
								// a a4 = a;
								// b b4 = b;
								// c;
								// com.tsf.shell.workspace3D.a.a(a4, b4, d);
								// throw exception;
							} catch (Exception e) {

							} finally {

							}
						}
					};
					widgetInfo.r = true;
					SharedData.getGLHandler().queueEvent(r);
					bool = true;
				}
			} else {
				widgetInfo.s = true;
			}
		}
		return bool;
	}

	void aaa(final AppWidgetInfo widgetInfo, final int screen) {
		widgetInfo.r = false;
		if (widgetInfo.s) {
			widgetInfo.s = false;
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					a(widgetInfo, screen);
				}
			});
		}
	}

	public final void b() {
		this.b = false;
	}

	public final void b(int paramInt) {
		int j = this.c.size();
		for (int i = 0;; i++) {
			AppWidgetInfo widgetInfo;
			Bitmap localBitmap;
			if (i < j) {
				widgetInfo = (AppWidgetInfo) this.c.get(i);
				if (widgetInfo.mScreen != paramInt)
					continue;
				localBitmap = widgetInfo.mIcon;
				widgetInfo.mIcon = null;
				if ((localBitmap != null) && (!localBitmap.isRecycled()))
					;
			} else {
				return;
			}
			ScreenLayout.LayoutParams localLayoutParams = (ScreenLayout.LayoutParams) widgetInfo.mHostView
					.getLayoutParams();
			if (widgetInfo.p == null) {
				de localde = new de();
				widgetInfo.p = localde;
				localde.setWidget(widgetInfo);
			}
			widgetInfo.p.a(localLayoutParams, localBitmap, false);
			localBitmap.recycle();
		}
	}

	public final void b(AppWidgetInfo widgetInfo) {
		if (mWidgets.remove(widgetInfo))
			a(widgetInfo, 0);
	}

	public final void c() {
		this.b = true;
	}

	public final void c(AppWidgetInfo widgetInfo) {
		this.c.add(widgetInfo);
	}

	public final void d(AppWidgetInfo widgetInfo) {
		this.c.remove(widgetInfo);
	}
}
